/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swing.modelos;

import hibernate.GenericDAOHibernate;
import hibernate.exception.BussinessException;
import hibernate.util.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import org.hibernate.Transaction;

/**
 *
 * @author ariel
 */
public class ComboBoxModelo<T, ID extends Serializable> extends AbstractListModel implements ComboBoxModel{

    private ArrayList<T> lista;
    private GenericDAOHibernate<T,ID> dao;
    private T seleccion;
    private Transaction tx = null;

    public ComboBoxModelo(GenericDAOHibernate objetoDao){
        try {
            this.dao = objetoDao;
            this.dao.setSession(HibernateUtil.getSessionFactory());
            this.tx = this.dao.getSession().beginTransaction();
            lista = (ArrayList<T>) dao.findAll();
            this.tx.commit();
        } catch (BussinessException ex) {
            this.tx.rollback();
        } finally {
            this.dao.getSession().close();
        }
    }
    @Override
    public int getSize() {
        return lista.size();
    }

    @Override
    public Object getElementAt(int index) {
        return lista.get(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        seleccion = (T) anItem;
    }

    @Override
    public Object getSelectedItem() {
        return seleccion;
    }
    
    
}
